﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FourKillEnginePrototype
{
    public class Point3D
    {
        public Point3D(double x, double y) { X = x; Y = y; Z = 0; }
        public Point3D(double x, double y, double z) { X = x; Y = y; Z = z; }
        public double X;
        public double Y;
        public double Z;
        public double RelativeX;
        public double RelativeY;
        public double RelativeZ;
        public double RelativeAngleToX;
        public double RelativeAngle;
        public double RelativeDistance;
        public bool IsBehindPlayer;
        
    }
    public class Polygon3D
    {
        public Polygon3D(params Point3D[] points) { this.Points = new List<Point3D>(points); }
        public Polygon3D() { this.Points = new List<Point3D>(); }
        public List<Point3D> Points;

        public void UpdateRelativeCoordinates(double x, double y, double orientation)
        {
            foreach (Point3D point in Points) 
            {
                double translatedX = (point.X - x),
                    translatedY = (point.Y - y),
                    reverseOrientation = -1 * orientation;

                point.RelativeX = (Math.Cos(reverseOrientation) * translatedX) - (Math.Sin(reverseOrientation) * translatedY);
                point.RelativeY = (Math.Sin(reverseOrientation) * translatedX) + (Math.Cos(reverseOrientation) * translatedY);

                point.RelativeAngleToX = Math.Atan2(Math.Abs(point.RelativeY), Math.Abs(point.RelativeX)) * (point.RelativeX > 0 && point.RelativeY < 0 ? -1d : 1d);
                if (point.RelativeX < 0 && point.RelativeY < 0) point.RelativeAngle = Math.Abs(point.RelativeAngleToX) + Math.PI;
                else if (point.RelativeX > 0 && point.RelativeY < 0) point.RelativeAngle = 2 * Math.PI - Math.Abs(point.RelativeAngleToX);
                else if (point.RelativeX < 0 && point.RelativeY > 0) point.RelativeAngle = Math.PI - Math.Abs(point.RelativeAngleToX);


                point.IsBehindPlayer = point.RelativeX <= 0;
                point.RelativeDistance = Math.Sqrt(Math.Pow(point.RelativeX, 2) + Math.Pow(point.RelativeY, 2));
            }
        }
    }
}
